<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{../../lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{../../css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<!--
        layui-form 表示这是个from表单样式
        layui-form-item：表示fromn表单的每一项

-->
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">父级部门编号</label>
        <div class="layui-input-inline">
            <!-- lay-search=""，可以让下拉框附带搜索功能 -->
            <select name="pid" id="pidSelectId_add" lay-filter="pid" lay-search="" lay-verify="required"
                    lay-reqtext="父级部门编号不能为空" placeholder="请选择父级部门编号"></select>
            <!--  <input type="text" name="pid" lay-verify="required" lay-reqtext="父级部门编号不能为空" placeholder="请输入父级部门编号" value="" class="layui-input">-->
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">部门名称</label>
        <div class="layui-input-inline">
            <input type="text" name="title" lay-verify="required" lay-reqtext="部门名称不能为空" placeholder="请输入部门名称" value=""
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">备注</label>
        <div class="layui-input-inline">
            <!--            lay-verify="required" 表示当前是必填项
                            lay-reqtext="手机不能为空" 验证不通过的提示信息，对应出现
            -->
            <input type="text" name="remark" lay-verify="required" lay-reqtext="备注不能为空" placeholder="请输入备注" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">地址</label>
        <div class="layui-input-inline">
            <input type="text" name="address" lay-verify="required" lay-reqtext="地址不能为空" placeholder="请输入地址" value=""
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script th:src="@{../../lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    <!-- 加载from表单模块-->
    layui.use(['form', 'jquery'], function () {
        //form表单对象
        var form = layui.form,
            //layer弹出框对象
            layer = layui.layer,
            //jquery对象
            $ = layui.$;

        // ajax调用接口，查询部门信息，生成下拉框
        $.ajax({
            //动态获取下拉选项的接口，返回数据主要是id+name
            url: 'deptSelect',
            dataType: 'json',
            type: 'get',
            success: function (data) {
                console.log(data)
                var deptList = data.data
                let str = "";
                for (let dept of deptList) {
                    //组装数据
                    str += `<option value='${dept.id}'>${dept.title}</option>`;
                }
                //jquery赋值方式，将拼接的字符串赋值
                $("#pidSelectId_add").html(str);
                //重新渲染生效
                form.render();
            }
        });

        /**
         * form的监听提交
         *   on(参数一,参数二)：事件
         *     参数一: submit(saveBtn)：对按钮进行绑定点击事件
         *     参数二：function (data) { ... } 回调函数
         */
        form.on('submit(saveBtn)', function (data) {
            layer.alert(JSON.stringify(data))

            // ajax请求，调用后台新增接口
            $.ajax({
                url: 'add',
                type: 'post',
                contentType: 'application/json;charset=UTF-8',
                data: JSON.stringify(data.field),
                dataType: 'json',
                success: function (data) {


                }
            });


            var index = layer.alert(
                JSON.stringify(data.field),
                {title: '最终的提交信息'},
                function () {
                    // 关闭弹出层
                    layer.close(index);

                });

            // 关闭新增页面层
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);

            // 刷新table列表
            parent.window.location.reload();

            return false;
        });
    });
</script>
</body>
</html>